package com.zhonghui.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.mes.domain.vo.MesProductionPlanVo;
import com.zhonghui.response.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.mes.domain.MesProductionPlan;
import com.zhonghui.mes.service.IMesProductionPlanService;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.common.core.page.TableDataInfo;

/**
 * 生产计划Controller
 * 
 * @author zhonghui
 * @date 2022-05-21
 */
@Api(tags="制造执行MES-生产计划")
@RestController
@RequestMapping("/mes/plan")
public class MesProductionPlanController extends BaseController
{
    @Autowired
    private IMesProductionPlanService mesProductionPlanService;

    /**
     * 查询生产计划列表
     */
    @ApiOperation("查询生产计划列表")
    @GetMapping("/list")
    public TableDataInfo<List<MesProductionPlanVo>> list(MesProductionPlan mesProductionPlan)
    {
        startPage();
        List<MesProductionPlanVo> list = mesProductionPlanService.selectMesProductionPlanList(mesProductionPlan);
        return getDataTable(list);
    }

    /**
     * 导出生产计划列表
     */
    @ApiOperation("导出生产计划列表")
    @Log(title = "生产计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductionPlan mesProductionPlan)
    {
        List<MesProductionPlanVo> list = mesProductionPlanService.selectMesProductionPlanList(mesProductionPlan);
        ExcelUtil<MesProductionPlanVo> util = new ExcelUtil<MesProductionPlanVo>(MesProductionPlanVo.class);
        util.exportExcel(response, list, "生产计划数据");
    }

    /**
     * 获取生产计划详细信息
     */
    @ApiOperation("获取生产计划详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<MesProductionPlanVo> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(mesProductionPlanService.selectMesProductionPlanById(id));
    }

    /**
     * 新增生产计划
     */
    @ApiOperation("新增生产计划")
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody MesProductionPlan mesProductionPlan)
    {
        mesProductionPlan.setApplicantStatus(0);
        return BaseResult.success(mesProductionPlanService.insertMesProductionPlan(mesProductionPlan));
    }

    /**
     * 修改生产计划
     */
    @ApiOperation("修改生产计划")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody MesProductionPlan mesProductionPlan)
    {
        mesProductionPlan.setApplicantStatus(0);
        mesProductionPlan.setReviewer(null);
        mesProductionPlan.setReviewerStatus(null);
        mesProductionPlan.setReviewerDate(null);
        mesProductionPlan.setReviewComments("");
        return BaseResult.success(mesProductionPlanService.updateMesProductionPlan(mesProductionPlan));
    }

    /**
     * 提交生产计划
     */
    @ApiOperation("提交生产计划")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody MesProductionPlan mesProductionPlan)
    {
        // 申请状态 0未提交 1待审核 2已审核
        mesProductionPlan.setApplicantStatus(1);
        mesProductionPlan.setApplicantDate(DateUtils.getNowDate());
        if(mesProductionPlan.getId() != null) {
            // 修改提交
            MesProductionPlanVo mesProductionPlanVo = mesProductionPlanService.selectMesProductionPlanById(mesProductionPlan.getId());
            if (mesProductionPlanVo == null) {
                return BaseResult.error("此生产计划不存在，不能提交！");
            }
            mesProductionPlan.setReviewer(null);
            mesProductionPlan.setReviewerStatus(null);
            mesProductionPlan.setReviewerDate(null);
            mesProductionPlan.setReviewComments("");
            return BaseResult.success(mesProductionPlanService.updateMesProductionPlan(mesProductionPlan));
        } else {
            return BaseResult.success(mesProductionPlanService.insertMesProductionPlan(mesProductionPlan));
        }
    }

    /**
     * 生产计划审核
     */
    @ApiOperation("生产计划审核")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody MesProductionPlan mesProductionPlan)
    {
        mesProductionPlan.setApplicantStatus(2);
        mesProductionPlan.setReviewer(SecurityUtils.getUserId());
        mesProductionPlan.setReviewerDate(DateUtils.getNowDate());
        return BaseResult.success(mesProductionPlanService.updateMesProductionPlan(mesProductionPlan));
    }

    /**
     * 删除生产计划
     */
    @ApiOperation("删除生产计划")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable String[] ids)
    {
        return BaseResult.success(mesProductionPlanService.deleteMesProductionPlanByIds(ids));
    }
}
